package com.hengxinguotong.zhihuichengjian.ui.lift.data;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/2/26
 */

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.Lift;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.ui.lift.OnFragmentInteractionListener;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/2/21
 *
 * 升降机数据
 */

public class LiftDataDetailActivity extends BaseActivity implements View.OnClickListener, OnFragmentInteractionListener {
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.lift_img)
    ImageView liftImg;
    @BindView(R.id.lift_box)
    ImageView liftBox;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private float imageHeight;//升降机图片高度

    private Lift lift;
    private List<String> mTitleDataList;
    private List<BaseFragment> fragments;
    private DeviceListPagerAdapter deviceListPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lift_data_detail);
        ButterKnife.bind(this);
        lift = (Lift) getIntent().getSerializableExtra("lift");
        initView();
    }

    private void initView() {
        titleTv.setText(lift.getName());

        ViewTreeObserver vto = liftImg.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                liftImg.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                imageHeight = liftImg.getHeight();
                int topMargin = (int) ((186f/284f)*imageHeight);
                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) liftBox.getLayoutParams();
                layoutParams.setMargins(0,topMargin, 0, 0);
                liftBox.setLayoutParams(layoutParams);
            }
        });

        initViewPager();
    }

    private void initViewPager() {
        viewPager.setOffscreenPageLimit(6);
        fragments = new ArrayList<BaseFragment>();
        fragments.add(LiftDataLiveFragment.newInstance(lift.getId()));
        fragments.add(LiftDataViolationFragment.newInstance(lift.getId()));
        fragments.add(LiftDataWarningFragment.newInstance(lift.getId()));
        fragments.add(LiftDataHistoryFragment.newInstance(lift.getId()));
        fragments.add(LiftDataLiftRecycleFragment.newInstance(lift.getId()));
        fragments.add(LiftDataStatisticsFragment.newInstance(lift.getId()));
        deviceListPagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(deviceListPagerAdapter);


        mTitleDataList = new ArrayList<String>();
        mTitleDataList.add("实时数据");
        mTitleDataList.add("违章数据");
        mTitleDataList.add("预警数据");
        mTitleDataList.add("历史数据");
        mTitleDataList.add("工作循环");
        mTitleDataList.add("统计分析");

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleDataList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_666666));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_f288ae5));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.color_f288ae5));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return Utils.dip2px(LiftDataDetailActivity.this, 20);
            }
        });

        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(1.0f));
        fragmentContainerHelper.setDuration(300);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
            }
        });
    }

    @OnClick({R.id.back_tv})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_tv:
                finish();
                break;
        }
    }

    @Override
    public void onFragmentInteraction(int index, int count) {

    }

    @Override
    public void onUpdateLift(String heightPercentStr) {
        if(!Utils.isEmpty(heightPercentStr)){
            float heightPercent = Float.parseFloat(heightPercentStr);
            int topMargin = (int)((heightPercent/100)*imageHeight*(152f/284f));
            TranslateAnimation animation = new TranslateAnimation(0,0,0,-topMargin);
            long duration = (long) (heightPercent/100 * 1000);
            animation.setDuration(duration);
            animation.setFillAfter(true);
            liftBox.startAnimation(animation);
        }
    }
}
